<!DOCTYPE HTML>
<html>

<head>
    <meta charset="utf-8">
    <title>制作520520登录视频</title>
    <meta name="viewport" content="width=device-width,initial-scale=1, minimum-scale=1, maximum-scale=1, user-scalable=no" />
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="full-screen" content="true" />
    <meta name="screen-orientation" content="portrait" />
    <meta name="x5-fullscreen" content="true" />
    <meta name="360-fullscreen" content="true" />
    <style>
        html, body {
            -ms-touch-action: none;
            background: #888888;
            padding: 0;
            border: 0;
            margin: 0;
            height: 100%;
        }
    </style>
</head>

<body>

    <script>
        var getRequest = getRequest();
        console.log("getRequest", getRequest);
        if (getRequest.code) {
            this.code = getRequest.code;
            console.log("CODE：" + getRequest.code);
            sendCode(getRequest.code)
        } else {
              //TODO 此文件放在服务器中的地址
            let pageUrl = "https%3a%2f%2fchatwhy.com%2fdeepface%2fweb%2fcode.html%3f";
            console.log(pageUrl)
            // let pageUrl = "https://cutepard.com/code.html?";
            // pageUrl = encodeURI(pageUrl);
            // pageUrl = addUrlInfo(pageUrl);

            // console.log("pageUrl", pageUrl);
            var appid = "wx56cb3da83ffebcad";
            var url =
                "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + appid + "&redirect_uri=" + pageUrl + "&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect"
           // window.location.href = url;
        }

        function addUrlInfo(jumpurl) {


            var userid = getQueryString("userid");
            console.log("userid----" + userid);
            if (userid) {
                jumpurl = jumpurl + encodeURIComponent("userid=" + userid);
            } else {
                jumpurl = jumpurl + encodeURIComponent("userid=" + null);
            }

            var goodid = getQueryString("goodid");
            console.log("goodid----" + goodid);
            if (goodid) {

                jumpurl = jumpurl + encodeURIComponent("&goodid=" + goodid);
            } else {
                jumpurl = jumpurl + encodeURIComponent("&goodid=" + null);
            }


            return jumpurl;
        }

        function getRequest() {
            // return { code: "code" }
            var code = getQueryString("code");
            return { code: code }
        }

        function sendCode(code) {
            var httpRequest = new XMLHttpRequest();//第一步：建立所需的对象

            //TODO 修改接口
            httpRequest.open('POST', "https://chatwhy.com/deepface/login?code=" + code);//第二步：打开连接  将请求参数写在url中  ps:"./Ptest.php?name=test&nameone=testone"

            httpRequest.send();//第三步：发送请求  将请求参数写在URL中
            /**
             * 获取数据后的处理程序
             */
            httpRequest.onreadystatechange = function () {
                if (httpRequest.readyState == 4 && httpRequest.status == 200) {
                    var json = httpRequest.responseText;//获取到json字符串，还需解析
                    var objInfo = JSON.parse(json);
                    console.log("后台的code回调消息", objInfo);
                    let userinfo = {
                        name: "哈哈",
                        headUrl: "https://cutepard.com/rank_server/static/example_user.png",
                        openid: "t12456486",
                    }
                    userinfo.name = objInfo.data.nickname;
                    userinfo.headUrl = objInfo.data.headimgurl;
                    userinfo.openid = objInfo.data.openid;

                    //TODO 修改为我的游戏的地址
                    jumpurl = "https://chatwhy.com/deepface/web/index.html?"

                    jumpurl = jumpurl + ("&name=" + userinfo.name);

                    jumpurl = jumpurl + ("&headUrl=" + userinfo.headUrl);

                    jumpurl = jumpurl + ("&openid=" + userinfo.openid);

                    // jumpurl = addUrlObj(jumpurl)
                    // console.log("jumpurl------", jumpurl);

                    window.location.href = jumpurl;
					// console.log(jumpurl);

                };
            };
        }

        function addUrlObj(jumpurl) {
            var userid = getQueryString("userid");
            console.log("userid----" + userid);
            if (userid) {
                jumpurl = jumpurl + ("&userid=" + userid);
            } else {
                jumpurl = jumpurl + ("&userid=" + null);
            }

            var goodid = getQueryString("goodid");
            console.log("goodid----" + goodid);
            if (goodid) {

                jumpurl = jumpurl + ("&goodid=" + goodid);
            } else {
                jumpurl = jumpurl + ("&goodid=" + null);
            }
            return jumpurl;
        }

        function getQueryString(name) {
            let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
            let r = window.location.search.substr(1).match(reg);
            if (r != null) return decodeURIComponent(r[2]);
            return null;
        }

        function getQueryStringMultiple(name) {
            let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
            if (window.location.hash.indexOf("?") < 0) {
                return null;
            }
            let r = window.location.hash.split("?")[1].match(reg);
            if (r != null) return decodeURIComponent(r[2]);
            return null;
        }


    </script>


</body>

</html>
